#include <iostream>
using namespace std;

class Date
{
public:
	Date()
	{
		_year = 0;
		_month = 0;
		_day = 0;
	}
	Date(int year, int month, int day)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	int Getmonthday(int year, int month)
	{
		int array[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
			++array[2];
		return array[month];
	}
	Date& AfterXday(int x)
	{
		_day += x;
		int monthday = Getmonthday(_year, _month);
		while (_day > monthday)
		{
			_day -= monthday;
			++_month;
			if (_month > 12)
			{
				++_year;
				_month = 1;
			}
			monthday = Getmonthday(_year, _month);
		}
		return *this;
	}


	int _year;
	int _month;
	int _day;
};

int main()
{
	int num;
	cin >> num;
	int year = 0, month = 0, day = 0, x = 0;

	int i = 0;
	while (num--)
	{
		cin >> year >> month >> day >> x;
		Date d(year, month, day);
		d.AfterXday(x);
		printf("%04d-%02d-%02d\n", d._year, d._month, d._day);
	}

	return 0;
}
